package cn.newtouch._07_tran;

import org.apache.rocketmq.client.producer.LocalTransactionState;
import org.apache.rocketmq.client.producer.TransactionListener;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.Date;


public class MyTranListener implements TransactionListener {
    //
    @Override
    public LocalTransactionState executeLocalTransaction(Message message, Object o) {
        try {
            //执行业务，保存本地事务
            System.out.println("执行成功了，提交事务！！" + o);
            System.out.println(new Date().toLocaleString());
            return LocalTransactionState.COMMIT_MESSAGE;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("有问题！！回滚事务！！");
            return LocalTransactionState.ROLLBACK_MESSAGE;
        }
    }

    //回查事务方法
    @Override
    public LocalTransactionState checkLocalTransaction(MessageExt messageExt) {
        //这里查询本地事务状态
        return LocalTransactionState.COMMIT_MESSAGE;
    }
}
